/*
 * BillManageView.java
 */
package billmanage;

import com.breeze.entity.Tbill;
import com.breeze.entity.Tcategory;
import com.breeze.entity.Tuser;
import com.breeze.entity.Tuserbill;
import com.breeze.service.ICoreService;
import com.breeze.util.Constants;
import com.breeze.util.PageList;
import com.breeze.util.SpringUtil;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * The application's main frame.
 */
public class BillManageView extends FrameView {

    public BillManageView(SingleFrameApplication app) {
        super(app);

        initComponents();
        coreService = (ICoreService) SpringUtil.getBean("coreService");
        example.setPageList(pageList);
        initCategory();
        initTable();
        initPrevAndNext();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = BillManageApp.getApplication().getMainFrame();
            aboutBox = new BillManageAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        BillManageApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        billTable = new javax.swing.JTable();
        l_prev = new javax.swing.JLabel();
        l_next = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cb_category = new javax.swing.JComboBox();
        cb_active = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        cb_settled = new javax.swing.JCheckBox();
        cb_invalid = new javax.swing.JCheckBox();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 727, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(billmanage.BillManageApp.class).getContext().getResourceMap(BillManageView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        jMenuItem4.setText(resourceMap.getString("jMenuItem4.text")); // NOI18N
        jMenuItem4.setName("jMenuItem4"); // NOI18N
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem4);

        jMenuItem3.setText(resourceMap.getString("jMenuItem3.text")); // NOI18N
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem3);

        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem2);

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem1);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(billmanage.BillManageApp.class).getContext().getActionMap(BillManageView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        billTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        billTable.setName("billTable"); // NOI18N
        jScrollPane1.setViewportView(billTable);

        l_prev.setText(resourceMap.getString("l_prev.text")); // NOI18N
        l_prev.setName("l_prev"); // NOI18N

        l_next.setText(resourceMap.getString("l_next.text")); // NOI18N
        l_next.setName("l_next"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        cb_category.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cb_category.setName("cb_category"); // NOI18N

        cb_active.setText(resourceMap.getString("cb_active.text")); // NOI18N
        cb_active.setName("cb_active"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jButton1.setAction(actionMap.get("Search")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        cb_settled.setText(resourceMap.getString("cb_settled.text")); // NOI18N
        cb_settled.setName("cb_settled"); // NOI18N

        cb_invalid.setText(resourceMap.getString("cb_invalid.text")); // NOI18N
        cb_invalid.setName("cb_invalid"); // NOI18N

        jButton2.setAction(actionMap.get("delete")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("settle")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 708, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 3, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(statusPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(statusMessageLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 404, Short.MAX_VALUE)
                            .addComponent(statusAnimationLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(statusPanelLayout.createSequentialGroup()
                                    .addGap(19, 19, 19)
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(cb_category, 0, 294, Short.MAX_VALUE))
                                .addGroup(statusPanelLayout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(l_prev)))
                            .addGap(49, 49, 49)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1)
                        .addGap(58, 58, 58)
                        .addComponent(jButton2)
                        .addGap(21, 21, 21)))
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(l_next)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton3)
                            .addGroup(statusPanelLayout.createSequentialGroup()
                                .addComponent(cb_active)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cb_settled)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cb_invalid)))))
                .addGap(104, 104, 104))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addComponent(l_prev)
                        .addGap(9, 9, 9)
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(cb_category, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addComponent(l_next)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cb_active)
                            .addComponent(jLabel4)
                            .addComponent(cb_settled)
                            .addComponent(cb_invalid))))
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(statusPanelLayout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(statusMessageLabel)
                                .addComponent(statusAnimationLabel))
                            .addGap(20, 20, 20))
                        .addGroup(statusPanelLayout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton2)
                                .addComponent(jButton1))
                            .addGap(22, 22, 22)))
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addContainerGap())))
        );

        progressBar.setName("progressBar"); // NOI18N

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        JFrame mainFrame = BillManageApp.getApplication().getMainFrame();
        AddBillFrm frm=new AddBillFrm();
        frm.setLocationRelativeTo(mainFrame);
        frm.setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        JFrame mainFrame = BillManageApp.getApplication().getMainFrame();
        CategoryFrm frm=new CategoryFrm();
        frm.setLocationRelativeTo(mainFrame);
        frm.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        JFrame mainFrame = BillManageApp.getApplication().getMainFrame();
        UserFrm frm=new UserFrm();
        frm.setLocationRelativeTo(mainFrame);
        frm.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
         JFrame mainFrame = BillManageApp.getApplication().getMainFrame();
        SettleDlg dlg=new SettleDlg(mainFrame, true);
        dlg.setLocationRelativeTo(mainFrame);
        dlg.setVisible(true);
        searchBill(null);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable billTable;
    private javax.swing.JCheckBox cb_active;
    private javax.swing.JComboBox cb_category;
    private javax.swing.JCheckBox cb_invalid;
    private javax.swing.JCheckBox cb_settled;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel l_next;
    private javax.swing.JLabel l_prev;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
    
    private String[] header=new String[]{"序列号","账单号","详细","金额(元)","类别","支付人","应支付人","状态","创建时间"};
    private ICoreService coreService;
    private Map categoryMap=new HashMap();
    private PageList pageList=new PageList();
    private Tbill example=new Tbill();

    private void test() {

        PropertyConfigurator.configure("src/config/log4j.properties");
        Logger.getLogger(BillManageView.class.getName()).log(Level.WARNING, null, "sss");

        ICoreService service = (ICoreService) SpringUtil.getBean("coreService");
        Tuser u = new Tuser();
        u.setUsername("aaa");
        u.setPassword("bbbb");
        service.insertUser(u);
    }
    
    private void initCategory(){
        List list=coreService.queryCategoryList(null);
        Object[] items=new Object[list.size()+1];
        items[0]="全部";
        for(int i=0;i<list.size();i++){
            Tcategory cate=(Tcategory)list.get(i);
            items[i+1]=cate.getDisplay();
            categoryMap.put(cate.getDisplay(), cate);
        }
        
        ComboBoxModel cbm=new DefaultComboBoxModel(items);
        cb_category.setModel(cbm);
    }
    
    private void initTable(){
        searchBill(null);
    }
    
    private void searchBill(String op)
    {
        Tcategory selectedCategory=(Tcategory)categoryMap.get(cb_category.getSelectedItem());
        if(selectedCategory!=null){
            example.setCategoryid(selectedCategory.getSeqid());
        }else{
            example.setCategoryid(null);
        }
        
        example.setState(null);
        if(cb_active.isSelected())
            example.setState("0");
        if(cb_invalid.isSelected())
            example.setState("2");
        if(cb_settled.isSelected())
            example.setState("1");
        if(op==null)
            pageList.setCurrentPage(1);
        else if("next".equals(op))
            pageList.nextPage();
        else if("prev".equals(op))
            pageList.prevPage();
        
        pageList.setTotalNum(coreService.queryBillCount(example));
        List res=coreService.queryBillList(example);
        Object[][] items=packData(res);
        
        TableModel tm=new DefaultTableModel(items,header);
        billTable.setModel(tm);
    }
    
    private Object[][] packData(List list){
        Object[][] items=new Object[list.size()][9];
        for(int i=0;i<list.size();i++){
            Tbill b=(Tbill)list.get(i);
            items[i][0]=b.getSeqid();
            items[i][1]=b.getBillno();
            items[i][2]=b.getRemark();
            items[i][3]=b.getFee();
            
            Tcategory cate=new Tcategory();
            cate.setSeqid(b.getCategoryid());
            cate=(Tcategory)coreService.queryCategoryList(cate).get(0);
            items[i][4]=cate.getDisplay();
            
            //支付人
            Tuser u=new Tuser();
            u.setSeqid(b.getPaymanid());
            u=(Tuser)coreService.queryUserList(u).get(0);
            items[i][5]=u.getUsername();
            
            //应支付人
            Tuserbill userbill=new Tuserbill();
            userbill.setBillid(b.getSeqid());
            List l=coreService.queryUserbillList(userbill);
            StringBuilder sb=new StringBuilder();
            for(Object o:l){
                Tuserbill bi=(Tuserbill)o;
                Tuser us=new Tuser();
                us.setSeqid(bi.getUserid());
                us=(Tuser)coreService.queryUserList(us).get(0);
                sb.append(us.getUsername()).append(";");
            }
            items[i][6]=sb.toString();
            
            //状态
            if("0".equals(b.getState()))
                items[i][7]="活跃";
            else if("1".equals(b.getState()))
                items[i][7]="已结算";
            else if("2".equals(b.getState()))
                items[i][7]="作废";
            
            try {
                items[i][8]=Constants.DATE_FORMAT_RE.format(Constants.DATE_FORMAT.parse(b.getCreateDt()));
            } catch (ParseException ex) {
                Logger.getLogger(BillManageView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return items;
    }

    @Action
    public void Search() {
        searchBill(null);
    }

    private void initPrevAndNext() {
        l_next.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                searchBill("next");
            }
        });
        
        l_prev.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                searchBill("prev");
            }
        });
    }

    @Action
    public void delete() {
        int selected=billTable.getSelectedRowCount();
        if(selected!=1){
            JOptionPane.showMessageDialog(null, "please select one record");
            return;
        }
        
        int selectedRow=billTable.getSelectedRow();
        int seqid=(Integer)billTable.getModel().getValueAt(selectedRow, 0);
        Tbill bill=new Tbill();
        bill.setSeqid(seqid);
        bill.setState(Constants.BILL_STATE_DELETED);
        coreService.updateBill(bill);
        JOptionPane.showMessageDialog(null, "successful!");
        searchBill(null);
    }

    @Action
    public void settle() {
        int selected=billTable.getSelectedRowCount();
        if(selected!=1){
            JOptionPane.showMessageDialog(null, "please select one record");
            return;
        }
        
        int selectedRow=billTable.getSelectedRow();
        int seqid=(Integer)billTable.getModel().getValueAt(selectedRow, 0);
        Tbill bill=new Tbill();
        bill.setSeqid(seqid);
        bill.setState(Constants.BILL_STATE_SETTLED);
        bill.setSettleDt(Constants.DATE_FORMAT.format(new Date()));
        coreService.updateBill(bill);
        JOptionPane.showMessageDialog(null, "successful!");
        searchBill(null);
    }
}
